<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BookcaseExhibition extends BaseModel
{
    use HasFactory;

    protected $table = 'bookcase_exhibition';

    /**
     * 获取书柜对应的所有的展览id
     * @param bookcase_id 书柜id
     */
    public function getExhibitionIdByBookcaseId($bookcase_id){
        return $this->where('bookcase_id' , $bookcase_id)->pluck('exhibition_id')->toArray();
    }

     /**
     * 获取展览对应的所有的书柜id
     * @param exhibition_id 展览id
     */
    public function getBookcaseIdByExhibitionId($exhibition_id){
        return $this->where('exhibition_id' , $exhibition_id)->pluck('bookcase_id')->toArray();
    }



    /**
     * 判断此机柜是否有展览中
     * @param id 展览id
     */
    public function bookcaseIsExhibition($id){
        $exhibition_id = $this->where('bookcase_id' , $id)->value('exhibition_id');
        if($exhibition_id){
            return  Exhibition::where('id' , $exhibition_id)->value('name');
        }

        return false;//未存在在任何展览中 
    }

     /**
     * 判断此展览是否在机柜中展览
     * @param id 展览id
     */
    public function exhibitionIsBookcase($id){
        $bookcase_id = $this->where('exhibition_id' , $id)->value('bookcase_id');
        if($bookcase_id){
            return  Bookcase::where('id' , $bookcase_id)->where('is_del' , 1)->value('name');
        }

        return false;//未存在在任何展览中 
    }



    /**
     * 根据机柜id，获取所有展览信息
     * @param bookcase_id 机柜id
     */
    public function getExhibitionAll($bookcase_id){
        // $exhibition_ids = $this->getExhibitionIdByBookcaseId($bookcase_id);
        // $exhibition_info = Exhibition::whereIn('id' , $exhibition_ids)->where('is_del' , 1)->get();
        
        // return $exhibition_info;

        $exhibition_list = $this->where('bookcase_id' , $bookcase_id)->get()->toArray();
        $exhibition_info = [];
        foreach($exhibition_list as $key=>$val){
            $exhibition_info = Exhibition::where('id' , $val['exhibition_id'])->select('name')->where('is_del' , 1)->first();
            if(empty($exhibition_info)){
                continue;
            }
            $exhibition_info['create_time'] = $val['create_time'];//这个是配置的时间，不是展览添加的时间
        }

        return $exhibition_info;
    }
     /**
     * 根据机柜id，获取所有展览的作品信息
     * @param bookcase_id 机柜id
     */
    public function getExhibitionAllProductionName($bookcase_id){
        $exhibition_ids = $this->getExhibitionIdByBookcaseId($bookcase_id);
        $exhibitionProductionModelObj = new ExhibitionProduction();
        $production_ids = $exhibitionProductionModelObj->getProductionIdByExhibitionId($exhibition_ids);
        
        $productionModelObj = new Production();
        $production_info = $productionModelObj->getProduction($production_ids);

        return $production_info;
    }


    /**
     * 展览添加机柜，删除多余的，增加新增的
     * @param exhibition_id 展览id
     * @param bookcase_ids 机柜id  多个 逗号 拼接
     * @param $type 类型  add 新增，直接增加   change 需要查询
     */
    public function exhibitionBookcaseChange($exhibition_id , $bookcase_ids , $type = 'change'){
        $bookcase_ids = empty($bookcase_ids) ? [] : (is_array($bookcase_ids) ? $bookcase_ids : explode(',' , $bookcase_ids));
        if($type == 'add'){
            $bookcase_ids_ed = [];
        }else{
            $bookcase_ids_ed = $this->getBookcaseIdByExhibitionId($exhibition_id);
        }

        $bookcase_ids_del = array_diff($bookcase_ids_ed,$bookcase_ids); //对比返回在 $bookcase_ids_ed 中但是不在 $bookcase_ids 的数据删除
        $bookcase_ids_add = array_diff($bookcase_ids,$bookcase_ids_ed); //对比返回在 $bookcase_ids 中但是不在 $bookcase_ids_ed 的数据添加
        $this->whereIn('bookcase_id' , $bookcase_ids_del)->where('exhibition_id' , $exhibition_id)->delete();
        $data = [];
        foreach($bookcase_ids_add as $key=>$val){
            $data[$key]['exhibition_id'] = $exhibition_id; 
            $data[$key]['bookcase_id'] = $val; 
            $data[$key]['create_time'] = date('Y-m-d H:i:s'); 
        }
        $this->insert($data);
    }


    /**
     * 机柜添加展览，删除多余的，增加新增的
     * @param bookcase_id 机柜id  多个 逗号 拼接
     * @param exhibition_ids 展览id  多个 逗号 拼接
     * @param $type 类型  add 新增，直接增加   change 需要查询
     */
    public function bookcaseExhibitionChange($bookcase_id , $exhibition_ids , $type = 'change'){
        $exhibition_ids = empty($exhibition_ids) ? [] : explode(',' , $exhibition_ids);
        if($type == 'add'){
            $exhibition_ids_ed = [];
        }else{
            $exhibition_ids_ed = $this->getExhibitionIdByBookcaseId($bookcase_id);
        }

        $exhibition_ids_del = array_diff($exhibition_ids_ed,$exhibition_ids); //对比返回在 $exhibition_ids_ed 中但是不在 $exhibition_ids 的数据删除
        $exhibition_ids_add = array_diff($exhibition_ids,$exhibition_ids_ed); //对比返回在 $exhibition_ids 中但是不在 $exhibition_ids_ed 的数据添加
        $this->whereIn('exhibition_id' , $exhibition_ids_del)->where('bookcase_id' , $bookcase_id)->delete();
        $data = [];
        foreach($exhibition_ids_add as $key=>$val){
            $data[$key]['bookcase_id'] = $bookcase_id; 
            $data[$key]['exhibition_id'] = $val; 
            $data[$key]['create_time'] = date('Y-m-d H:i:s'); 
        }
        $this->insert($data);
    }

    /**
     * 机柜 删除展览
     * @param $bookcase_id 机柜id
     * @param $exhibition_ids 展览id
     */
    public function bookcaseExhibitionDel($bookcase_id , $exhibition_ids){
        $exhibition_ids = explode(',' , $exhibition_ids);
        return $this->whereIn('exhibition_id' , $exhibition_ids)->where('bookcase_id' , $bookcase_id)->delete();
    }
     /**
     * 展览添加作品
     * @param $exhibition_id 展览id
     * @param $production_ids 作品id
     */
    public function bookcaseExhibitionAdd($bookcase_id , $exhibition_ids){
        $exhibition_ids = explode(',' , $exhibition_ids);
        foreach($exhibition_ids as $key=>$val){
            $data[$key]['exhibition_id']= $val;
            $data[$key]['bookcase_id']= $bookcase_id; 
            $data[$key]['create_time']= date('Y-m-d H:i:s');
        }
        return $this->insert($data);
    }


}
